package zcw.com.lib_leet_code;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by 朱城委 on 2021/11/30.<br><br>
 *
 * 中等：岛屿数量
 */
public class Topic200 {

    public static void main(String[] args) {
        Topic200 instance = new Topic200();
        char[][] array;

        array = new char[][] {
                {'1', '1', '1', '1', '0'},
                {'1', '1', '0', '1', '0'},
                {'1', '1', '0', '0', '0'},
                {'0', '0', '0', '0', '0'}
        };
        System.out.println(instance.numIslands(array));

        array = new char[][] {
                {'1', '1', '0', '0', '0'},
                {'1', '1', '0', '1', '0'},
                {'0', '0', '1', '0', '0'},
                {'0', '0', '0', '1', '1'}
        };
        System.out.println(instance.numIslands(array));
    }

    public int numIslands(char[][] grid) {
        if(grid == null || grid.length == 0) {
            return 0;
        }

        Queue<Integer> queue = new LinkedList<>();

        int counter = 0;
        int row = grid.length;
        int col = grid[0].length;
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                if(grid[i][j] == '1') {
                    counter++;

                    grid[i][j] = '0';
                    queue.add(i * col + j);

                    while (!queue.isEmpty()) {
                        int index = queue.poll();
                        int newRow = index / col;
                        int newCol = index % col;

                        if(newRow - 1 >= 0 && grid[newRow - 1][newCol] == '1') {
                            grid[newRow - 1][newCol] = '0';
                            queue.offer((newRow - 1) * col + newCol);
                        }

                        if(newRow + 1 < row && grid[newRow + 1][newCol] == '1') {
                            grid[newRow + 1][newCol] = '0';
                            queue.offer((newRow + 1) * col + newCol);
                        }

                        if(newCol - 1 >= 0 && grid[newRow][newCol - 1] == '1') {
                            grid[newRow][newCol - 1] = '0';
                            queue.offer(newRow * col + newCol - 1);
                        }

                        if(newCol + 1 < col && grid[newRow][newCol + 1] == '1') {
                            grid[newRow][newCol + 1] = '0';
                            queue.offer(newRow * col + newCol + 1);
                        }
                    }
                }
            }
        }

        return counter;
    }

    public int numIslands2(char[][] grid) {
        if(grid == null || grid.length == 0) {
            return 0;
        }

        int counter = 0;
        for(int i = 0; i < grid.length; i++) {
            for(int j = 0; j < grid[0].length; j++) {
                if(grid[i][j] == '1') {
                    counter++;

                    helper(grid, i, j);
                }
            }
        }

        return counter;
    }

    private void helper(char[][] grid, int row, int col) {
        if(row < 0 || row >= grid.length || col < 0 || col >= grid[0].length || grid[row][col] == '0') {
            return ;
        }

        grid[row][col] = '0';
        helper(grid, row - 1, col);
        helper(grid, row + 1, col);
        helper(grid, row, col - 1);
        helper(grid, row, col + 1);
    }
}
